package me.sealer.ssm.controller;

import me.sealer.ssm.constant.GiteeConstant;
import me.sealer.ssm.constant.GithubConstant;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * 功能描述： $ OpenAuth2Controller
 *
 * @author sealer
 * @email 1178884049@qq.com
 * @date 2019年12月12日 11时28分10秒
 */
@Controller
@RequestMapping("/oauth2")
public class OpenAuth2Controller {
    @Resource(name = "restTemplate")
    private RestTemplate restTemplate;

    private static final String MY_GITHUB_CLIENT_ID = "8ffda36a06ccb0fe74a3";
    private static final String MY_GITHUB_CLIENT_SECRET = "853188b07a757e0c99205138bf56e94d44c41d74";

    private static final String MY_GITEE_CLIENT_ID = "96999b21ee76311576c8bf0e1441035d4b062c74b2158cdaaffa08ae8385189b";
    private static final String MY_GITEE_CLIENT_SECRET = "e4446ee18d1c0f71313f1b7d3453dc7bf07855f1a6e68d58874ad7c3a618d043";

    @RequestMapping("/code/github")
    public ModelAndView githubLogin() {
        String redirectUrl = "redirect:"
                + GithubConstant.GITHUB_AUTHORIZE_URL
                + "?"
                + GithubConstant.CLIENT_ID
                + "="
                + MY_GITHUB_CLIENT_ID
                + "&"
                + GithubConstant.SCOPE
                + "="
                + "read:user";
        return new ModelAndView(redirectUrl);
    }

    @RequestMapping("/code/gitee")
    public ModelAndView giteeLogin() {
        String redirectUrl = "redirect:"
                + GiteeConstant.GITEE_AUTHORIZE_URL
                + "?"
                + GiteeConstant.CLIENT_ID
                + "="
                + MY_GITEE_CLIENT_ID
                + "&"
                + GiteeConstant.REDIRECT_URI
                + "="
                + "http://localhost:8888/ssm/callback/gitee"
                + "&"
                + "response_type=code";
        return new ModelAndView(redirectUrl);
    }
}
